package engine.systems.ui.drawer
{
	import ash.core.Entity;
	
	import ash.core.Component;
	
	import org.osflash.signals.Signal;
	
	public class Drawer extends Component
	{
		public static const OPENED_STATE:int 	= 0;
		public static const CLOSED_STATE:int 	= 1;
		public static const OPENING_STATE:int 	= 2;
		public static const CLOSING_STATE:int 	= 3;
		
		internal var _invalidate:Boolean = false;
		internal var _state:int;
		
		public var openingTime:Number;
		public var closingTime:Number;
		
		public var openingVars:Object;
		public var closingVars:Object;
		
		public var opened:Signal = new Signal(Entity);
		public var closed:Signal = new Signal(Entity);
		
		public function Drawer(open:Boolean = false, openingVars:Object = null, closingVars:Object = null, openingTime:Number = 0.5, closingTime:Number = 0.5)
		{
			this._state = open ? Drawer.OPENED_STATE : Drawer.CLOSED_STATE;
			
			this.openingVars = openingVars;
			this.closingVars = closingVars;
			
			this.openingTime = openingTime;
			this.closingTime = closingTime;
		}
		
		override public function destroy():void
		{
			this.opened.removeAll();
			this.opened = null;
			
			this.closed.removeAll();
			this.closed = null;
			
			this.openingVars = null;
			this.closingVars = null;
		}
		
		public function get state():int { return this._state; }
		
		public function set open(open:Boolean):void
		{
			if(this._state > 1) return;
			
			this._state = open ? OPENED_STATE : CLOSED_STATE;
		}
		
		public function toggle():void
		{
			if(this._state == OPENED_STATE)
			{
				this._state == CLOSING_STATE;
				this._invalidate = true;
			}
			else if(this._state == CLOSED_STATE)
			{
				this._state = OPENING_STATE;
				this._invalidate = true;
			}
		}
	}
}